package com.zdkj.subway.check.service.impl.subwayRat.reportMondify;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.subway.check.entity.*;
import com.zdkj.subway.check.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表差错信息
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ReportMistakeInfoServiceImpl {

    @Resource
    private SwStReportErrorAlterAppayMapper reportMistakeInfoMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SwParamLineMapper swPmLineMapper;
    @Resource
    private SwParamStationMapper swParmStationMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SwPmReportPathMapper swPmReportPathMapper;

    /**
     * 报表差错信息查询
     * @param page
     * @param param
     * @return
     */
    public IPage<SwStReportErrorAlterApply> getReportErrorList(Page page, Map<String,Object> param) {
        /*String p_line = param.get("lineCode");
        String p_station = param.get("stationCode");
        if(StringUtils.isNotEmpty(p_line) || StringUtils.isNotEmpty(p_station)){
            List<Map<String, Object>> maps = lineOrstationNotnull(param);
            return maps;
        }
        Map<String,Object> p1=new HashMap<>();
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        if(sysUsers.size()<=0){

        }
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation=StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
        List<Map<String,Object>> reportErrorAlterAppays=new ArrayList<>();
        for(int i=0;i<managementLineStations.size();i++){
            JSONObject jsonObject = managementLineStations.getJSONObject(i);
            String line = jsonObject.getString("line");
            String station = jsonObject.getString("station");
            param.put("lineCode",line);
            param.put("stationCode",station);
            List<Map<String,Object>> reportErrorAlterAppayOne = reportMistakeInfoMapper.getReportErrorList(param);
            reportErrorAlterAppays.addAll(reportErrorAlterAppayOne);
        }*/
        IPage<SwStReportErrorAlterApply> reportErrorAlterAppayOne = reportMistakeInfoMapper.getReportErrorList(page,param);
        return reportErrorAlterAppayOne;
    }

    private List<Map<String,Object>> lineOrstationNotnull(Map<String,String> param){
        List<Map<String,Object>> reportErrorAlterAppayOne = reportMistakeInfoMapper.getReportErrorList(param);
        return reportErrorAlterAppayOne;
    }


    public List<SwParamLine> getLineLists(Map<String, String> param) {
        Map<String,Object> p1=new HashMap<>();
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        SysUser sysUser = sysUsers.get(0);
        if(sysUser.getBelongToLine()){
            String managementLineStation = sysUser.getManagementLineStation();
            managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
            JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
            List<String> lineCodes=new ArrayList<>();
            for(int i=0;i<managementLineStations.size();i++){
                JSONObject jsonObject = managementLineStations.getJSONObject(i);
                String line = jsonObject.getString("line");
                lineCodes.add(line);
            }
            QueryWrapper<SwParamLine> queryWrapper=new QueryWrapper<>();
            queryWrapper.in("CODE",lineCodes);
            List<SwParamLine> swPmLines = swPmLineMapper.selectList(queryWrapper);
            return swPmLines;
        }else {
            String managementLineStation = sysUser.getManagementLineStation();
            if(StringUtils.isEmpty(managementLineStation)){
                QueryWrapper<SwParamLine> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("CODE",sysUser.getLineCode());
                List<SwParamLine> swPmLines = swPmLineMapper.selectList(queryWrapper);
                return swPmLines;
            }else {
                managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
                JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
                List<String> lineCodes=new ArrayList<>();
                for(int i=0;i<managementLineStations.size();i++){
                    JSONObject jsonObject = managementLineStations.getJSONObject(i);
                    String line = jsonObject.getString("line");
                    lineCodes.add(line);
                }
                QueryWrapper<SwParamLine> queryWrapper=new QueryWrapper<>();
                queryWrapper.in("CODE",lineCodes);
                List<SwParamLine> swPmLines = swPmLineMapper.selectList(queryWrapper);
                return swPmLines;
            }
        }
    }

    /**
     * 车站信息
     * @param param
     * @return
     */
    public List<SwParamStation> getStationLists(Map<String, String> param) {
        Map<String,Object> p1=new HashMap<>();
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
        List<String> stationCode=new ArrayList<>();
        for(int i=0;i<managementLineStations.size();i++){
            JSONObject jsonObject = managementLineStations.getJSONObject(i);
            String line = jsonObject.getString("line");
            String station = jsonObject.getString("station");
            if(line.equals(param.get("lineCode")) && StringUtils.isNotEmpty(station)){
                stationCode.add(station);
            }
        }
        Map<String,Object> p2=new HashMap<>();
        p2.put("lineCode",param.get("lineCode"));
        p2.put("stationCodes",stationCode);

        List<SwParamStation> stationLists = swParmStationMapper.getStationLists(p2);
        return stationLists;
    }

    /**
     * 加入报表差错
     * @param swStReportErrorAlterApply
     * @param param
     * @return
     */
    public Integer addReportError(SwStReportErrorAlterApply swStReportErrorAlterApply,Map<String,String> param) {
        Map<String,Object> p1=new HashMap<>();
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        SysUser sysUser = sysUsers.get(0);
        swStReportErrorAlterApply.setCreatorId(sysUser.getUserId());
        swStReportErrorAlterApply.setCreatorName(sysUser.getNikename());
        Integer insert = reportMistakeInfoMapper.insertReport(swStReportErrorAlterApply);
        return insert;
    }

    /**
     * 获取 值班站长，值班员，站务员，
     * @param param
     * @return
     */
    public List<Map<String, Object>> getPersonRoles(Map<String, String> param) {
        Map<String, Object> p=new HashMap<>();
        p.put("role_code",param.get("roleCode"));
        List<SysRole> sysRoles = sysRoleMapper.selectByMap(p);
        SysRole sysRole = sysRoles.get(0);
        Map<String,Object> p2=new HashMap<>();

        p2.put("roleId",sysRole.getRoleId());
        if(null != param.get("stationCode") && !"".equals(param.get("stationCode")) && !"null".equals(param.get("stationCode")))
            p2.put("stationCode",param.get("stationCode"));
        if(null != param.get("lineCode") && !"".equals(param.get("lineCode")) && !"null".equals(param.get("lineCode")))
            p2.put("lineCode",param.get("lineCode"));
        List<Map<String, Object>> users=sysRoleMapper.getCurrRoleUsers(p2);
        return users;
    }

    /**
     * 获取报表路径参数管理
     * @param param
     * @return
     */
    public List<SwPmReportPath> getReports(Map<String, String> param) {
        List<SwPmReportPath> swPmReportPaths = swPmReportPathMapper.selectByMap(null);
        return swPmReportPaths;
    }



    public void updateReportApplyStatus(List param,String checkStatus) {
    }

    /**
     * 报表差错设定期限
     * @param checkStatus
     */
    public void updateReportApplyModifyLimitTime(JSONArray checkStatus) {
        for(Object obj : checkStatus){
            SwStReportErrorAlterApply swStReportErrorAlterApply = JSONObject.parseObject(obj.toString(),SwStReportErrorAlterApply.class);
            reportMistakeInfoMapper.updateById(swStReportErrorAlterApply);
        }
    }

}
